home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / rsx.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  4KB  |  137 lines

  1.  
  2. /* RSX.C command  --> Read in a Single line & eXecute "command"
  3.  *                    w/ subtitutions.
  4.  *
  5.  * Author: Jack Ekwall 1 May 89
  6.  *
  7.  * Works Better if You Compile w/o WildCard Expansion.
  8.  *
  9.  * Copyrighted to the Public Domain.  Unlimited Distribution Authorized.
  10.  *
  11.  * Last Update: 7 August 90/EK
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <stdek.h>
  19. #include <stdlib.h>
  20.  
  21. char *Documentation[] = {
  22.     "",
  23. "Usage:",
  24. "       RSX [/Fc] Command [Arguements] --> Do Command w/ Parameter Passing.",
  25.     "",
  26. "Options:",
  27. "         /Fc ---> Use 'c' as the Field Delimited vice Spaces.",
  28. "                  The $### format for Decimal ASCII is Legal.",
  29.     "",
  30. "Action:",
  31. "    1. RSX reads a Single Line of Text from Stdin.",
  32. "    2. Using the Field Delimiter, Slots $1-$9 are stuffed.  ($0 is the ",
  33. "       whole Line.)",
  34. "    3. RSX Spawns a Sub-Shell, passing to it a \"Command Line\" made up ",
  35. "       from Passed Parameters w/ these modifications:",
  36. "          A. Contents of Field Slots get substitued for $Field Tags.",
  37. "          B. Bangs (!) and Dits (') become (|) & (\"), respecitively.",
  38. "          C. $$ becomes $.",
  39. "          D. DOS WildCards are Passed Unchanged.",
  40. "    4. RSX Repeats the Above for Until Stdin Runs Dry.",
  41.     "",
  42.     NULL};
  43.  
  44. /* Declare Prototypes */
  45. void Usage(void);
  46.  
  47. main (int argc, char *argv[])
  48. {
  49.    int c, i, Flag = FALSE, Dlim = SPACE , Ok = TRUE;
  50.    BYTE Command[128], CmdLine[128];             /* Cmd B4 & After Stuffing */
  51.    char *tp1, *tp2, *tp3;                       /* Text Pointers */
  52.    BYTE *Text, Slot[11][128];                   /* Line & Words fm Stdin */
  53.    FILE *fp1, *fp2;                             /* Read, Write & Batch */
  54.  
  55.    Text = Slot[0];
  56.  
  57.  /* Check for /Option */
  58.     if (open("\\STD ERR", 1) >= 0) exit(1);
  59.     if (argc IS 1) Usage();
  60.     tp1 = argv[1];
  61.     if (*tp1++ IS '/') {
  62.        if (toupper(*tp1++) != 'F') Usage();
  63.        if (argc IS 3) Usage(); }
  64.        if ((c = (BYTE) *tp1++) IS '$') {
  65.           for (c = 0; (*tp1 >= '0') && (*tp1 <= '9'); tp1++)
  66.              c = 10 * c + *tp1 - 48;
  67.           if ((c IS 0) && (*tp1 IS '$')) { c = '$';  tp1++; }
  68.        }
  69.        if (c IS 0) Usage();
  70.        if (*tp1 IS NULL) { Dlim = c;
  71.  
  72.     /* SHIFT */
  73.        for (i = 1, --argc; i < argc + 1; i++) argv[i] = argv[i+1];
  74.     }
  75.  
  76.  /* Build Command$ */
  77.     for (i = 1, tp1 = Command; i < argc; i++) {
  78.        tp2 = argv[i];
  79.        while (*tp2 != NULL) {
  80.           c = *tp2++;
  81.           if (c IS BANG) c = BAR;
  82.           if (c IS DIT) c = QUOTE;
  83.           *tp1++ = c;
  84.        }
  85.        *tp1++ = SPACE;
  86.     }
  87.     tp1--; *tp1 = NULL;
  88.  
  89.  /* Check for Pipe on Stdin */
  90.     if (!INFLOW_EXISTS) Usage();
  91.  
  92.  /* Do Business */
  93.     while (gets(Text) != NULL) {
  94.  
  95.     /* Parse Text$ into Slot$[] by CHR$(Dlim) */
  96.        if (*Text IS NULL) continue;     /* Skip MT Line */
  97.        for (i = 1; i < 10; i++) *Slot[i] = NULL;
  98.        i = 1; Flag = FALSE; tp1 = Text; tp2 = Slot[i++];
  99.        while ((c = *tp1++) != NULL) {
  100.           if (c != Dlim) { Flag = TRUE; *tp2++ = c; continue; }
  101.           if (Flag IS TRUE) {
  102.              *tp2 = NULL; Flag = FALSE;
  103.              if (i < 9) { tp2 = Slot[i++]; continue; }
  104.              *tp2++ = c; Flag = TRUE;   /* Spill XS Text into $9 */
  105.           }
  106.        }
  107.        *tp2 = NULL;
  108.  
  109.     /* Build Command Line & Swap In Slot$[] */
  110.        for ( tp1 = Command, tp2 = CmdLine; (c = *tp1++) != NULL; ) {
  111.           *tp2++ = c;
  112.           if (c IS '$') {
  113.              if (*tp1 IS '$') { tp1++; continue; }      /* $$ ==> $ */
  114.              if ((*tp1 < '0') || (*tp1 > '9')) continue;
  115.              strcpy(--tp2,Slot[*tp1++ - '0']);
  116.              tp2 = CmdLine + strlen(CmdLine);
  117.           }
  118.        }
  119.        *tp2 = NULL;
  120.  
  121.     /* Shell Out & Do Command Line */
  122.        if (system(CmdLine) != 0) Ok = FALSE;
  123.        if (open("\\STD ERR", 1) >= 0) exit(1);
  124.     }
  125.  
  126.     exit(Ok);
  127. }
  128.  
  129. void Usage(void)
  130. {
  131.     char   **dp = Documentation;
  132.  
  133.     for ( ; *dp; dp++) fprintf(stderr,"%s\n", *dp);
  134.     exit(1);
  135. }
  136.  
  137.